package handles

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"vehicle-mgr/models"
)

func GoodAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good_price, _ := strconv.Atoi(c.PostForm("good_price"))

	if good_code == "" || good_name == "" || good_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.Good{

		GoodCode:  good_code,
		GoodName:  good_name,
		GoodPrice: good_price,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func GoodOrderAdd(c *gin.Context) {

	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	gid, _ := strconv.Atoi(c.PostForm("gid"))

	if order_code == "" || order_name == "" || order_price == 0 || gid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.GoodOrder{

		OrderCode:  order_code,
		OrderName:  order_name,
		OrderPrice: order_price,
		Gid:        gid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func GoodOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	/*
		SELECT
		  good_orders.id AS OrderId,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_price AS  GoodPrice,
		  good_orders.order_code AS OrderCode,
		  good_orders.order_name AS OrderName,
		  good_orders.order_price AS OrderPrice
		FROM
		  goods
		  LEFT JOIN good_orders ON goods.id = good_orders.gid
	*/
	var good []models.GoodOrderList

	models.DB.Table("goods").
		Select(
			"good_orders.id AS OrderId",
			"goods.good_code AS GoodCode",
			"goods.good_name AS GoodName",
			"goods.good_price AS  GoodPrice",
			"good_orders.order_code AS OrderCode",
			"good_orders.order_name AS OrderName",
			"good_orders.order_price AS OrderPrice",
		).
		Joins("LEFT JOIN good_orders ON goods.id = good_orders.gid").
		Offset(offset).Limit(size).Find(&good)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": good,
	})
	return
}

func GoodOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  good_orders.id AS OrderId,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_price AS  GoodPrice,
		  good_orders.order_code AS OrderCode,
		  good_orders.order_name AS OrderName,
		  good_orders.order_price AS OrderPrice
		FROM
		  goods
		  LEFT JOIN good_orders ON goods.id = good_orders.gid
	*/
	var good models.GoodOrderList

	models.DB.Table("goods").
		Select(
			"good_orders.id AS OrderId",
			"goods.good_code AS GoodCode",
			"goods.good_name AS GoodName",
			"goods.good_price AS  GoodPrice",
			"good_orders.order_code AS OrderCode",
			"good_orders.order_name AS OrderName",
			"good_orders.order_price AS OrderPrice",
		).
		Joins("LEFT JOIN good_orders ON goods.id = good_orders.gid").
		Where("good_orders.id=?", id).Find(&good)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": good,
	})
	return
}
